<template>
  <div class="app-container">
    <div class="configItem">
      <el-descriptions class="margin-top" title="基本配置" :column="3" border>
        <template slot="extra">
          <el-button type="primary" size="small" @click="sendArticle">推送文章</el-button>
          <el-button type="primary" size="small" @click="addTaskExam">发起考试</el-button>
        </template>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user" />
            模板名称
          </template>
          {{ configInfo.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone" />
            创建人
          </template>
          {{ configInfo.operatorName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline" />
            适用部门
          </template>
          <el-tag v-if="configInfo.deptList.length == 0" type="warning">
            未设置任何部门
          </el-tag>
          <el-tag v-for="(item,index) in configInfo.deptList" v-else :key="index" size="mini" style="margin: 0 3px">
            {{ item.name }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets" />
            文章库
          </template>
          共<span class="importantFont" style="margin: 0 3px">{{ configInfo.articleCount }}</span>篇文章
          <el-tooltip class="item" effect="dark" content="修改文库" placement="right">
            <div class="edit" @click="changeConfig('article')" />
          </el-tooltip>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building" />
            题库
          </template>
          共<span class="importantFont" style="margin: 0 3px">{{ configInfo.questionCount }}</span>道题目
          <el-tooltip class="item" effect="dark" content="修改题库" placement="right">
            <div class="edit" @click="changeConfig('question')" />
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <div class="configItem">
      <el-descriptions class="margin-top" title="积分配置" :column="4" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user" />
            每日总积分上限
          </template>
          {{ configDetail.max_score }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-mobile-phone" />
            积分模块
          </template>
          <ul>
            <li v-for="(item,index) in configDetail.scoreItem">
              模块名:{{ item.itemName }} || 单次得分：{{ item.itemScore }} || 积分上限 :{{ item.maxScore }} 分
            </li>
          </ul>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline" />
            答题策略
          </template>
          每天
          <span v-for="(item,index) in configDetail.questionList">
            {{ item.count }} 道 <el-tag effect="dark" size="mini">{{ getTypeName(item.qtype) }}</el-tag> ;
          </span>
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <div class="configItem">
      <el-descriptions class="margin-top" title="个性化配置" :column="4" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user" />
            日答题次数上限
          </template>
          {{ configDetail.answer_chance }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user" />
            错题提示
          </template>
          {{ configDetail.error_tips }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline" />
            答题提示
          </template>
          {{ configDetail.tips_opened }}
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <el-dialog
      title="发起考试"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <div>
        <el-form ref="taskInfo" :model="taskInfo" label-width="80px">
          <el-form-item label="考试名称">
            <el-input v-model="taskInfo.name" />
          </el-form-item>
          <el-form-item label="推送文案">
            <el-input v-model="taskInfo.message" />
          </el-form-item>
          <el-form-item label="推送范围">
            <el-button type="primary" plain size="mini" @click="empDialogVisible = true">已选择{{ taskInfo.empList.length }}人</el-button>
          </el-form-item>
          <el-form-item label="题目选择">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              multiple
              :limit="1"
              :file-list="fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip" style="color: red" @click="downLoad">点此下载上传模板</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="选择人员"
      :visible.sync="empDialogVisible"
      width="50%"
    >
      <emp-select-config-list @getEmpList="setEmpList" />
    </el-dialog>
  </div>
</template>
<script>
import { getTemplateDetail, downLoadTemplate } from '@/api/template'
import empSelectConfigList from '@/views/template/components/empSelectConfigList'
export default {
  components: {
    empSelectConfigList
  },
  data() {
    return {
      configInfo: '',
      configDetail: {},
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      taskInfo: {
        name: '',
        type: '',
        empList: [],
        message: ''
      },
      fileList: [],
      dialogVisible: false,
      empDialogVisible: false,
      editImg: require('../../assets/common/edit.png')
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      const tid = this.$route.params.id
      getTemplateDetail(tid).then(res => {
        this.configInfo = res.data
        this.configDetail = JSON.parse(this.configInfo.configInfo)
      })
    },
    changeConfig(type) {
      const data = {
        templateId: this.configInfo.id,
        configType: type
      }
      if (type === 'question') {
        this.$router.push({
          name: 'templateQuestionConfigDetail',
          query: data
        })
      } else if (type === 'article') {
        this.$router.push({
          name: 'templateArticleConfigDetail',
          query: data
        })
      }
    },
    getTypeName(type) {
      if (type == 1) {
        return '选择题'
      } else if (type == 2) {
        return '填空题'
      } else {
        return '其他类型题目'
      }
    },
    addTaskExam() {
      this.dialogVisible = true
    },
    setEmpList(list) {
      this.taskInfo.empList = list
      this.empDialogVisible = false
    },
    downLoad() {
      downLoadTemplate().then(res => {
        const blob = new Blob([res], { type: 'application/x-xls' })
        const a = document.createElement('a')
        // 创建URL
        const blobUrl = window.URL.createObjectURL(blob)
        a.download = '题库导入模板.xlsx'
        a.href = blobUrl
        document.body.appendChild(a)
        // 下载文件
        a.click()
        // 释放内存
        URL.revokeObjectURL(blobUrl)
        document.body.removeChild(a)
      })
    }
  }
}
</script>
<style scoped>
.edit{
  background: url("../../assets/common/edit.png");
  position: relative;
  display: inline-block;
  width: 18px;
  top: 4px;
  left: 2px;
  height: 18px;
  background-size: 18px 18px;
  cursor: pointer;
}
.configItem{
  margin-bottom: 15px;
}
</style>
